package edu.uestc.fly.performancetest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.zeroxlab.kubench.Kubench;

import butterknife.BindView;
import butterknife.ButterKnife;
import edu.uestc.fly.performancetest.constant.Constant;
import edu.uestc.fly.performancetest.cpu.CPUAsyncTask;
import edu.uestc.fly.performancetest.io.DBManager;
import edu.uestc.fly.performancetest.io.IOAsyncTask;
import edu.uestc.fly.performancetest.mem.MEMAsyncTask;
import edu.uestc.fly.performancetest.utils.ProgressListen;
import edu.uestc.fly.performancetest.utils.RoundProgressBarWidthNumber;

public class MainActivity extends AppCompatActivity implements ProgressListen {

    Context context  = this;
    CPUAsyncTask cpuTask = new CPUAsyncTask();
    MEMAsyncTask memTask = new MEMAsyncTask();
    IOAsyncTask ioTask;
    long totalScore = 0;

    @BindView(R.id.cpuBtn) Button cpuBtn;
    @BindView(R.id.memBtn) Button memBtn;
    @BindView(R.id.ioBtn) Button ioBtn;
    @BindView(R.id.gpuBtn) Button gpuBtn;
    @BindView(R.id.cpuScore) TextView cpuScore;
    @BindView(R.id.memScore) TextView memScore;
    @BindView(R.id.ioScore) TextView ioScore;
    @BindView(R.id.gpuScore) TextView gpuScore;
    @BindView(R.id.totalScore) TextView totalScoreTV;
    @BindView(R.id.tvProgressAlert) TextView alertText;
    @BindView(R.id.alertLay) LinearLayout alertLay;
    @BindView(R.id.alertBg) LinearLayout alertBg;
    @BindView(R.id.progressbar)
    RoundProgressBarWidthNumber mProgressBar;
    private DBManager dbManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);


        totalScoreTV.setVisibility(View.INVISIBLE);
        restoreProgressBar();

        initDB();


    }

    private void initDB() {
        dbManager = new DBManager(this);
    }

    public void restoreProgressBar(){
        mProgressBar.setProgress(0);
        alertLay.setVisibility(View.GONE);
        alertBg.setVisibility(View.GONE);
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.cpuBtn:
                cpuTask.progresslistener = this;
                cpuTask.execute();
                break;
            case R.id.memBtn:
                memTask.progresslistener = this;
                memTask.execute();
                break;
            case R.id.ioBtn:
                ioTask = new IOAsyncTask(this);
                ioTask.progresslistener = this;
                ioTask.execute();
                break;
            case R.id.gpuBtn:
                //魔方功能
                startActivity(new Intent(this,Kubench.class));
                break;

        }
    }

    @Override
    public void initProgress(Constant.TaskType type, int progress) {

        switch (type){
            case CPU:
                alertLay.setVisibility(View.VISIBLE);
                alertBg.setVisibility(View.VISIBLE);
                cpuBtn.setEnabled(false);
                mProgressBar.setProgress(progress);
                alertText.setText("正在测试CPU性能....");
                break;
            case MEM:
                alertLay.setVisibility(View.VISIBLE);
                alertBg.setVisibility(View.VISIBLE);
                memBtn.setEnabled(false);
                mProgressBar.setProgress(progress);
                alertText.setText("正在测试MEM性能....");
                break;
            case IO:
                alertLay.setVisibility(View.VISIBLE);
                alertBg.setVisibility(View.VISIBLE);
                ioBtn.setEnabled(false);
                mProgressBar.setProgress(progress);
                alertText.setText("正在测试IO性能....");
                break;
            case GPU:
                break;
        }


    }

    @Override
    public void updateProgress(Constant.TaskType type,int progress) {

        mProgressBar.setProgress(progress);
    }

    @Override
    public void closeProgress(Constant.TaskType type,double time) {

        int score = 0;
        switch (type){
            case CPU:
                score = (int)(1.0 / time * 100000000.0) ;
                totalScore += score;
                totalScoreTV.setText("总得分："+String.valueOf(totalScore));
                cpuScore.setText(String.valueOf(score));
                cpuScore.setVisibility(View.VISIBLE);
                alertLay.setVisibility(View.GONE);
                alertBg.setVisibility(View.GONE);
                cpuBtn.setEnabled(true);
                break;
            case MEM:
                score = (int)(1.0 / time * 100000000.0) ;
                totalScore += score;
                totalScoreTV.setText("总得分："+String.valueOf(totalScore));
                memScore.setText(String.valueOf(score));
                memScore.setVisibility(View.VISIBLE);
                alertLay.setVisibility(View.GONE);
                alertBg.setVisibility(View.GONE);
                memBtn.setEnabled(true);
                break;
            case IO:
                score = (int)(1.0 / time * 100000000.0) ;
                totalScore += score;
                totalScoreTV.setText("总得分："+String.valueOf(totalScore));
                ioScore.setText(String.valueOf(score));
                ioScore.setVisibility(View.VISIBLE);
                alertLay.setVisibility(View.GONE);
                alertBg.setVisibility(View.GONE);
                ioBtn.setEnabled(true);
                break;
            case GPU:
                break;
        }
        //显示总得分
        if (totalScore > 0 ){
            totalScoreTV.setVisibility(View.VISIBLE);
        }

    }



    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        dbManager.closeDB();// 释放数据库资源
    }




}
